package com.android.launcher3.setting;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.android.launcher3.BaseActivity;
import com.android.launcher3.Launcher;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;

public class LauncherSettingActivity extends BaseActivity {

    private static final String TAG = "LauncherSettingActivity";

    private final static double SCALE = 2;

    private LinearLayout layoutContainer;

    private View icBackToHome;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        setContentView(R.layout.launcher_setting_activity);
        setUpView();
    }

    private void setUpView() {
        layoutContainer = findViewById(R.id.layout_container);

        icBackToHome = findViewById(R.id.ic_back_to_home);
        icBackToHome.setOnClickListener(v -> finish());
        findViewById(R.id.layout_general_settings).setOnClickListener(v -> {
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            startActivity(intent);
        });
        findViewById(R.id.layout_wifi).setOnClickListener(v -> {
            Log.d("TinkerLauncher3", "onWifiClick");
            Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
            startActivity(intent);
        });
        findViewById(R.id.layout_bluetooth).setOnClickListener(v -> {
            String pkg = "com.applepie.bluetooth";
            String activityName = "com.applepie.bluetoothcall.MainActivity";
            ComponentName component = new ComponentName(pkg, activityName);
            Intent intent = new Intent();
            intent.setComponent(component);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        });

        findViewById(R.id.layout_language).setOnClickListener(v -> {
            Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
            startActivity(intent);
        });

        findViewById(R.id.layout_developer).setOnClickListener(v -> {
            Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
            startActivity(intent);
        });

        findViewById(R.id.layout_wallpaper).setOnClickListener(v -> {
            Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
            startActivity(intent);
        });

        findViewById(R.id.widget_help).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(LauncherSettingActivity.this, LauncherHelpActivity.class);
//                startActivity(intent);
            }
        });

        findViewById(R.id.widget_about).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(LauncherSettingActivity.this, LauncherAboutActivity.class);
//                startActivity(intent);
            }
        });
        findViewById(R.id.widget_updater).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // com.abfota.systemUpdate/com.redstone.ota.ui.activity.RsMainActivity
                try {
                    ComponentName componentName = new ComponentName("com.abfota.systemUpdate", "com.redstone.ota.ui.activity.RsMainActivity");
                    Intent intent = new Intent();
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setComponent(componentName);
                    intent.setPackage("com.abfota.systemUpdate");
                    startActivity(intent);
                } catch (Exception e) {
                }
            }
        });

        screenAdaptation();
    }

    private void screenAdaptation() {


        layoutContainer.post(() -> {

            DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

            int width = layoutContainer.getWidth();
            int height = layoutContainer.getHeight();

            double scale = (width * 1.0) / (height * 1.0);

            if (scale > SCALE) {
                width = (int) (height * SCALE);
            } else {
                height = (int) ((width * 1.0) / SCALE);
            }

            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layoutContainer.getLayoutParams();
            layoutParams.width = width;
            layoutParams.height = height;
            layoutContainer.setLayoutParams(layoutParams);


            if (Utilities.isLongStripScreen(getApplicationContext())) {
                LinearLayout.LayoutParams gapLayoutParams = new LinearLayout.LayoutParams(Utilities.pxFromDp(10, displayMetrics), Utilities.pxFromDp(10, getResources().getDisplayMetrics()));
                findViewById(R.id.gap1).setLayoutParams(gapLayoutParams);
                findViewById(R.id.gap2).setLayoutParams(gapLayoutParams);
                findViewById(R.id.gap3).setLayoutParams(gapLayoutParams);
                findViewById(R.id.gap4).setLayoutParams(gapLayoutParams);
                findViewById(R.id.gap5).setLayoutParams(gapLayoutParams);
                findViewById(R.id.gap6).setLayoutParams(gapLayoutParams);
                findViewById(R.id.gap7).setLayoutParams(gapLayoutParams);
                findViewById(R.id.gap8).setLayoutParams(gapLayoutParams);
                findViewById(R.id.gap9).setLayoutParams(gapLayoutParams);

                LinearLayout.LayoutParams icBackToHomeLayoutParams = (LinearLayout.LayoutParams) icBackToHome.getLayoutParams();

                icBackToHomeLayoutParams.width = Utilities.pxFromDp(97.5f, displayMetrics);
                icBackToHomeLayoutParams.height = Utilities.pxFromDp(60, displayMetrics);
                icBackToHome.setLayoutParams(icBackToHomeLayoutParams);

            }
        });


    }

}
